//设置cookie
export const setCookie = (name, value, remainTime) => {
    let d = new Date();
    d.setTime(d.getTime() + (remainTime * 1000));
    let expires = "expires=" + d.toUTCString();
    document.cookie = name + "=" + value + "; " + expires;
}

//获取cookie
export const getCookie = (cname) => {
    let name = cname + "=";
    let ca = document
        .cookie
        .split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1);
        if (c.indexOf(name) != -1) 
            return c.substring(name.length, c.length);
        }
    return "";
}

//清除指定名称的cookie
export const clearCookie = (name) => {
    setCookie(name, "", -1);
}

//清除所有cookie
export const clearAllCookie = () => {
    let keys = document
        .cookie
        .match(/[^ =;]+(?=\=)/g);
    // if (keys) { for (let i = keys.length; i--;)     document.cookie = keys[i] +
    // '=0;expires=' + new Date(0).toUTCString() }
    for (let item of keys) {
        clearCookie(item);
    }
}

// 获得url中的参数
export const getUrlParam = (name) => {
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}